let background = 'bg.jpg'
let bg_video = ''
let color = '0,0,0'
let tmd = '0.1'
let fcolor = '0,0,0'
let yj = '32'
let fh = true
let gk = true

window.wallpaperPropertyListener = {
    applyUserProperties: function (properties) {
        if (properties.background) {
            fbackground(properties.background.value)
        }

        if (properties.bg_video) {
            fvideo(properties.bg_video.value)
        }

        if (properties.color) {
            funcolor(properties.color.value)
        }

        if (properties.tmd) {
            ftmd(properties.tmd.value)
        }

        if (properties.fcolor) {
            funfcolor(properties.fcolor.value)
        }

        if (properties.yj) {
            fyj(properties.yj.value)
        }

        if (properties.fh) {
            fh = properties.fh.value
            document.getElementById('fh').style.display =
                fh ? 'block' : 'none'
        }

        if (properties.gk) {
            gk = properties.gk.value
            document.getElementById('dgk').style.display =
                gk ? 'block' : 'none'
        }
        console.log("Wallpaper Engine synced!")
    },
};

function fbackground(x) {
    background = x
    let i = (background == null || background == '')
        ? "url(\"bg.jpg\")" : "url(\"file:///" + background + "\")"
    document.body.style.backgroundImage = i
}

function funcolor(x) {
    color = x.split(' ')
    color = color.map(function (x) {
        return Math.ceil(x * 255)
    });
    const d = 'rgba(' + color + ',' + tmd + ')'
    document.getElementById('ti').style.backgroundColor = d
    document.getElementById('dgk').style.backgroundColor = d
    document.getElementById('tmb').style.backgroundColor = d
}

function ftmd(x) {
    tmd = x
    const d = 'rgba(' + color + ',' + tmd + ')'
    document.getElementById('ti').style.backgroundColor = d
    document.getElementById('dgk').style.backgroundColor = d
    document.getElementById('tmb').style.backgroundColor = d
    document.getElementById('ti').style.backdropFilter =
        tmd == 0 ? 'none' : 'blur(8px)'
    document.getElementById('dgk').style.backdropFilter =
        tmd == 0 ? 'none' : 'blur(8px)'
    document.getElementById('tmb').style.backdropFilter =
        tmd == 0 ? 'none' : 'blur(8px)'
}

function funfcolor(x) {
    fcolor = x.split(' ')
    fcolor = fcolor.map(function (x) {
        return Math.ceil(x * 255)
    });
    const t = 'rgb(' + fcolor + ')'
    document.getElementById('ti').style.color = t
    document.getElementById('dgk').style.color = t
    document.getElementById('tm_t').style.color = t
}

function fyj(x) {
    yj = x
    const t = yj + 'px'
    document.getElementById('ti').style.borderRadius = t
    const tt = (x == '0' || x == 0) ? '0' : '32px'
    document.getElementById('dgk').style.borderRadius = tt
    document.getElementById('tm_t').style.borderRadius = tt
}

function fvideo(x) {
    bg_video = x
    document.getElementById('video').src = "file:///" + x
    document.getElementById('video').style.display = x == '' ?
        "none" : "initial"
}